Fix escape characters of events when dry running

JSONEditor double escapes escape characters to properly display them in the UI, however when dry running Agents we
actually need to send the Event's characters as they were.
Before an event containing new lines like this `Line A\nLineB` would be send as `Line A\\nLineB` to the backend. By
replacing the double escaped slashes we ensure the event is handled as it would be when not dry running.

 #1677

Dominik Sander 7 years ago
parent
commit
4309460896

+ 1 - 1
app/assets/javascripts/components/utils.js.coffee

@@ -69,7 +69,7 @@ class @Utils
69 69
               json = $(e.target).find('.payload-editor').val()
70 70
               json = '{}' if json == ''
71 71
               try
72
-                payload = JSON.parse(json)
72
+                payload = JSON.parse(json.replace(/\\\\([n|r|t])/g, "\\$1"))
73 73
                 throw true unless payload.constructor is Object
74 74
                 if Object.keys(payload).length == 0
75 75
                   json = ''

+ 35 - 0
spec/features/dry_running_spec.rb

@@ -0,0 +1,35 @@
1
+require 'rails_helper'
2
+
3
+describe "Dry running an Agent", js: true do
4
+  let(:formatting_agent) { agents(:bob_formatting_agent) }
5
+  let(:user)    { users(:bob) }
6
+  let(:emitter) { agents(:bob_weather_agent) }
7
+
8
+  before(:each) do
9
+    login_as(user)
10
+  end
11
+
12
+  def open_dry_run_modal(agent)
13
+    visit edit_agent_path(agent)
14
+    click_on("Dry Run")
15
+    expect(page).to have_text('Event to send')
16
+  end
17
+
18
+  context 'successful dry runs' do
19
+    it 'sends escape characters correctly to the backend' do
20
+      emitter.events << Event.new(payload: {data: "Line 1\nLine 2\nLine 3"})
21
+      formatting_agent.sources << emitter
22
+      formatting_agent.options.merge!('instructions' => {'data' => "{{data | newline_to_br | strip_newlines | split: '<br />' | join: ','}}"})
23
+      formatting_agent.save!
24
+
25
+      open_dry_run_modal(formatting_agent)
26
+      find('.dry-run-event-sample').click
27
+      within(:css, '.modal .builder') do
28
+        expect(page).to have_text('Line 1\nLine 2\nLine 3')
29
+      end
30
+      click_on("Dry Run")
31
+      expect(page).to have_text('Line 1,Line 2,Line 3')
32
+      expect(page).to have_selector(:css, 'li[role="presentation"].active a[href="#tabEvents"]')
33
+    end
34
+  end
35
+end

+ 8 - 0
spec/fixtures/agents.yml

@@ -60,6 +60,14 @@ bob_weather_agent:
60 60
   keep_events_for: <%= 45.days %>
61 61
   options: <%= { :location => 94102, :lat => 37.779329, :lng => -122.41915, :api_key => 'test' }.to_json.inspect %>
62 62
 
63
+bob_formatting_agent:
64
+  type: Agents::EventFormattingAgent
65
+  user: bob
66
+  name: "Formatting Agent"
67
+  guid: <%= SecureRandom.hex %>
68
+  keep_events_for: <%= 45.days %>
69
+  options: <%= { instructions: {}, mode: 'clean' }.to_json.inspect %>
70
+
63 71
 jane_weather_agent:
64 72
   type: Agents::WeatherAgent
65 73
   user: jane

+ 1 - 1
spec/rails_helper.rb

@@ -13,7 +13,7 @@ require 'rspec/rails'
13 13
 require 'rr'
14 14
 require 'webmock/rspec'
15 15
 
16
-WebMock.disable_net_connect!
16
+WebMock.disable_net_connect!(allow_localhost: true)
17 17
 
18 18
 # Requires supporting ruby files with custom matchers and macros, etc,
19 19
 # in spec/support/ and its subdirectories.